package com.effective.java.equals;

public class GoodEqualsMethod {

	public static void main(String[] args) {

		Point p1 = new Point(1, 0);
		Point p2 = new Point(2, 1);
		Point p3 = new Point(1, 0);

		System.out.println("p1.equals(p2) = " + p1.equals(p2));
		System.out.println("p2.equals(p3) = " + p2.equals(p3));
		System.out.println("p1.equals(p3) = " + p1.equals(p3));
	}

	private static class Point {

		private final int x;
		private final int y;

		public Point(int x, int y) {

			this.x = x;
			this.y = y;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof Point)) {
				return false;
			}

			Point p = (Point) obj;
			return p.x == this.x && p.y == this.y;
		}
	}
}
